package com.zxj.camerademo.camera2;

import android.graphics.RectF;
import android.hardware.camera2.params.Face;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.ImageView;

import com.zxj.camerademo.R;
import com.zxj.camerademo.view.AutoFitTextureView;
import com.zxj.camerademo.view.FaceView;

import java.util.ArrayList;
import java.util.List;

public class Camera2FaceActivity extends AppCompatActivity implements View.OnClickListener, Camera2HelperFace.FaceDetectListener {

    private Camera2HelperFace camera2HelperFace;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2_face);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);

        AutoFitTextureView textureView = findViewById(R.id.textureView);
        camera2HelperFace = new Camera2HelperFace(this, textureView);
        camera2HelperFace.setFaceDetectListener(this);

        ImageButton btnTakePic = findViewById(R.id.btnTakePic);
        ImageView ivExchange = findViewById(R.id.ivExchange);

        btnTakePic.setOnClickListener(this);
        ivExchange.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btnTakePic:
                camera2HelperFace.takePic();
                break;
            case R.id.ivExchange:
                camera2HelperFace.exchangeCamera();
                break;
        }
    }

    @Override
    public void onFaceDetect(List<Face> faces, List<RectF> facesRect) {
        FaceView faceView = findViewById(R.id.faceView);
        faceView.setFaces(facesRect);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        camera2HelperFace.releaseCamera();
        camera2HelperFace.releaseThread();
    }
}
